package com.spacing.app;

import java.util.ArrayList;
import java.util.Locale;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;
import com.senseilearning.spanish.R;
import com.spacing.app.data.Chapters;
import com.spacing.app.data.Section;

public class WordsViewActivity extends SherlockListActivity implements OnInitListener{
	Section section;
	Typeface font_robo_l;
	Typeface font_robo_m;
	Typeface font_robo_m_i;
	private TextToSpeech tts;
	ArrayList<RowModel> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapters_list);
		Intent intent = getIntent();
		section = (Section) intent.getSerializableExtra("section");
		ActionBar ab = getSupportActionBar();
		ab.setTitle(section.getName());
		
		font_robo_l = Typeface.createFromAsset(getAssets(),
				"fonts/Roboto-Light.ttf");
		font_robo_m_i = Typeface.createFromAsset(getAssets(),
				"fonts/Roboto-MediumItalic.ttf");
		font_robo_m = Typeface.createFromAsset(getAssets(),
				"fonts/Roboto-Medium.ttf");
		// TODO Action bar met words1 only, words2 only en Words1 & words2
		// TODO klik op woord om vertaling te tonen/verbergen afhankelijk van
		tts = new TextToSpeech(this, this);

		list = new ArrayList<RowModel>();

		for (int i = 0; i < section.size(); i++) {
			list.add(new RowModel(section.get(i).getWord1(), section.get(i).getWord3()));
		}

		setListAdapter(new WordlistAdapter(list));
		// setListAdapter(new ArrayAdapter<String>(this,
		// android.R.layout.simple_list_item_1, wordlist_1));
	}
	
	
	//onStart and onStop are now only used for calling the analytics methods
	@Override
	public void onStart(){
		super.onStart();
		//New code for this method must be added before the easytracker line!
		EasyTracker.getInstance().activityStart(this);
		Tracker tracker = EasyTracker.getTracker();
		tracker.setCustomDimension(2, section.getName()); //misschien ook een chapter meegeven? dimension id voor een chapter is 1, voor een section 2.
		tracker.sendView();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		//New code for this method must be added before the easytracker line!
		EasyTracker.getInstance().activityStop(this);
	}
	
	
	public void onInit(int status) {
		// TODO Auto-generated method stub
		// TTS is successfully initialized
		if (status == TextToSpeech.SUCCESS) {
			// Setting speech language
			Locale loc = new Locale ("rus", "RUS");
			int result = tts.setLanguage(loc);
			// If your device doesn't support language you set above
			if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
				// Cook simple toast message with message
				Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
				Log.e("TTS", "Language is not supported");
			}

			else {

			}
			// TTS is not initialized properly
		} else {
			Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG).show();
			Log.e("TTS", "Initilization Failed");
		}
	}
	
	public void speak(String text){
		tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}
	
	
	

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		speak(list.get(position).word2);
	}

	private RowModel getModel(int position) {
		return (((WordlistAdapter) getListAdapter()).getItem(position));
	}

	class WordlistAdapter extends ArrayAdapter<RowModel> {
		WordlistAdapter(ArrayList<RowModel> list) {
			super(WordsViewActivity.this, R.layout.row_word, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			
			ViewWrapper wrapper;

			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();

				row = inflater.inflate(R.layout.row_word, parent, false);
				wrapper = new ViewWrapper(row);
				row.setTag(wrapper);
				
			} else {
				wrapper = (ViewWrapper) row.getTag();
			}
			
			
			RowModel model = getModel(position);
			wrapper.getLabel().setText(model.getWord1());
			wrapper.getLabel().setTypeface(font_robo_l);
			wrapper.getLabel2().setText(model.getWord2());


			return (row);
		}
	}
	
	class ViewWrapper {
		View base;
		RatingBar rate = null;
		TextView label = null;
		TextView label2 = null;
		
		ViewWrapper(View base) {
			this.base = base;
		}



		TextView getLabel() {
			if (label == null) {
				label = (TextView) base.findViewById(R.id.word1);
			}
			
			return (label);
		}
		TextView getLabel2() {
			if (label2 == null) {
				label2 = (TextView) base.findViewById(R.id.word2);
			}
			return (label2);
		}
		
	}
	
	@Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
	
	

	class RowModel {
		String word1;
		String word2;

		RowModel(String word1, String word2) {
			this.word1 = word2;
			this.word2 = word1;
		}

		public String getWord1() {
			return word1;
		}

		public String getWord2() {
			return word2;
		}
	}

}